name: Deploy to VPS

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 16.16.0
          cache: npm

      - name: Install
        run: npm ci

      - name: Build
        run: npm run build

      - name: Set up SSH
        uses: webfactory/ssh-agent@v0.8.0
        with:
          ssh-private-key: ${{ secrets.ACTIONS_KEY }}

      - name: Check is dist exist
        run:  ssh -o StrictHostKeyChecking=no ${{ secrets.ACTIONS_ACCOUNT }}@${{ secrets.SERVER_IP }} "mkdir -p ${{ secrets.PROJECT_PATH }}"

      - name: Deploy to Server
        run: |
          scp -P ${{ secrets.SERVER_PORT }} -o StrictHostKeyChecking=no -r dist ${{ secrets.ACTIONS_ACCOUNT }}@${{ secrets.SERVER_IP }}:${{ secrets.PROJECT_PATH }}